###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# ControlPanel/Output/RemoveCategories.pm.pl -> RemoveCategories module       #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package Skin::CP::RemoveCategories;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Standard.pm.pl";

use HTML::Dialog;
use strict;

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless ($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = (input => undef, @_);

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Dialog = HTML::Dialog->new();

  my @CID = $SL::CGI->param('CID');

  my @Categories;
  foreach my $id (@CID) {
    my $category = $in{'input'}->{'CATEGORIES'}->[$in{'input'}->{'CATEGORIES_IX'}->{$id}];
    push(@Categories, $category->{'FULL_TITLE'});
  }
  
  my $Body = $Dialog->FormTextBox(
    value   => join("<br>", @Categories),
    subject => "Categories"
  );
  
  $Body .= $Dialog->TableRow(items => [{ body => "What do you want to do with subcategories and resources in the above categories?", colspan => 2 }]);
  
  $Body .= $Dialog->FormRadio(
    name    => "FORM_ACTION",
    value   => "REMOVE",
    items   => [
      { value => "REMOVE", label => "Remove them." },
      { value => "MOVE", label => "Move them to:" }
    ], join => "<br>"
  );
  
  my @options;
  CATEGORY: foreach my $category (@{ $in{'input'}->{'CATEGORIES'} }) {
    foreach my $id (@CID) {
      my $cat = $in{'input'}->{'CATEGORIES'}->[$in{'input'}->{'CATEGORIES_IX'}->{$id}];
      next CATEGORY if ($category->{'ROOT'} =~ /^$cat->{'ROOT'},$cat->{'ID'}/);
      next CATEGORY if ($category->{'ID'} == $id);
    }
    push(@options, { value => $category->{'ID'}, label => $category->{'FULL_TITLE'} });
  }

  $Body .= $Dialog->FormSelectBox(
    name      => "FORM_MOVE",
    items     => \@options
  );

  $Body .= $Dialog->FormButton(
    items => [
      { type => "submit", value => "Remove" },
      { type => "reset", value => "Cancel" }
    ], join => "&nbsp;"
  );
  
  $Body = $Dialog->Table(body => $Body);
  $Body = $Dialog->StandardPage(
    body    => $Body,
    header  => "Remove Categories",
    form    => 1,
    hiddens => [
      { name => "Referer", value => $SL::QUERY{'Referer'} },
      { name => "CID", value => [$SL::CGI->param('CID')] },
      { name => "action", value => "DoRemoveCategories" },
      { name => "CP", value => "1" },
      { name => "SID", value => $SL::ADMIN{'SID'} }
    ]
  );

  return $Body;
}

###############################################################################
# do subroutine
sub do {
  my $self = shift;
  my %in = (input => undef, @_);

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Dialog = HTML::Dialog->new();

  return $Dialog->StandardPage(
    body    => qq~The selected categories have been removed.<p>Click <a href="~.($SL::QUERY{'Referer'} || "$SYSTEM->{'SCRIPT_URL'}?CP=1&action=Categories&SID=$SL::ADMIN{'SID'}").qq~">here</a> if you're not redirected in 5 seconds.~,
    header  => "Remove Categories",
    metas   => [
      { http_equiv => "refresh", content => "5; url=".($SL::QUERY{'Referer'} || "$SYSTEM->{'SCRIPT_URL'}?CP=1&action=Categories&SID=$SL::ADMIN{'SID'}") }
    ]
  );
}

1;